<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 14:44
 */
namespace app\admin\controller;

use think\controller;
class User extends Admin
{
    protected $model;

    public function _initialize()
    {
        $this->model = model('user');
    }

    //用户列表页面
    public function userList()
    {
        $res = $this->model->selectAll();
        /*foreach($res as $val){
            if($val['login_count']==0){
                $val['last_login_time']="未登录过";
            }
        }*/
        $this->assign('res',$res);
        return view('user_list');
    }

    //用户添加页面
    public function userAddPage()
    {
        $res = $this->model->selectAll();
        $this->assign('res',$res);
        return view('user_add_page');
    }

    //用户添加
    public function userAdd()
    {
        $data['username'] = input('username');
        $data['password'] = input('password');
        $data['email'] = input('email');
        $data['mobile'] = input('mobile');
        $data['avatar'] = input('avatar');
        $data['avatar_address'] = input('avatar_address');
        $result = $this->model->addUser($data);
        if($result>0){
            echo "<script>alert('添加成功');window.location.href = 'userlist'</script>";
        }else{
            echo "<script>alert('添加失败');window.history.go(-1)</script>";
        }
    }

    //用户修改页面
    public function userUpdatePage()
    {
        $id = input('id');
        $res = $this->model->updateUser($id);
        $this->assign('res',$res);
        return view('user_update_page');
    }

    //用户更新
    public function userUpdate()
    {
        $id = input('id');
        $data['username'] = input('username');
        $data['password'] = input('password');
        $data['email'] = input('email');
        $data['mobile'] = input('mobile');
        $data['avatar'] = input('avatar');
        $data['username'] = input('username');
        $res = $this->model->editUser($data,$id);
        if($res>0){
            echo "<script>alert('修改成功');window.location.href = 'userlist'</script>";
        }else{
            echo "<script>alert('修改失败');window.history.go(-1)</script>";
        }
    }

    //用户删除
    public function userDelete()
    {
        $id = input('id');
        $res = $this->model->deleteUser($id);
        if($res>0){
            echo "<script>alert('删除成功');window.location.href = 'userlist'</script>";
        }
    }
}